package com.simple.relaxed.manager.impl;

import com.simple.relaxed.manager.AbstractManager;
import ohos.app.dispatcher.task.Revocable;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * Config管理类，所有作为配置的类，都会作为Bean保存在此管理类中，此类中的对象，会在程序初始化时，在非UI堆栈中执行
 */
public class ConfigManager extends AbstractManager<Class<?>, List<Revocable>> {

    private static ConfigManager configManager = new ConfigManager();

    private ConfigManager() {
        super();
    }

    public static ConfigManager getInstance() {
        if (configManager == null) {
            configManager = new ConfigManager();
        }
        return configManager;
    }

    @Override
    public void clear() {
        Iterator<Map.Entry<Class<?>, List<Revocable>>> iterator = map.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Class<?>, List<Revocable>> next = iterator.next();
            List<Revocable> revocables = next.getValue();
            revocables.removeIf(revocable -> {
                revocable.revoke();
                return true;
            });
            iterator.remove();
        }
    }
}
